package com.lc.question.dao;

import com.lc.question.pojo.Plan;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository("planDao")
public interface IPlanDao {

    //查询所有计划
    @Select("select * from plan where pub_Id=#{pub_Id}")
    public List<Plan> planAll(Plan plan);

    //查询针对某个对象群体和某个状态的所有计划
    @Select("<script>" +
            "select * from plan where 1=1 and pub_Id=#{pub_Id}" +
            "<if test='obj_user!=null and obj_user!=\"\"'>"+
                "and obj_user=#{obj_user}"+
            "</if>"+
            "<if test='plan_state!=null and plan_state!=\"\"'>"+
                "and plan_state=#{plan_state}"+
            "</if>"+
            "</script>")
    public List<Plan> findPlanByAnything(Plan plan);

    //查询针对某个对象群体和某个状态的所有计划
    @Select("<script>" +
            "select count(*) from plan where 1=1 and pub_Id=#{pub_Id}" +
            "<if test='obj_user!=null and obj_user!=\"\"'>"+
                "and obj_user=#{obj_user}"+
            "</if>"+
            "<if test='plan_state!=null and plan_state!=\"\"'>"+
                "and plan_state=#{plan_state}"+
            "</if>"+
            "</script>")
    public int planTotal(Plan plan);

    @Select("<script>" +
            "select * from plan where 1=1 and plan.pub_Id=#{plan.pub_Id}" +
            "<if test='plan.obj_user!=null and plan.obj_user!=\"\"'>"+
                "and plan.obj_user=#{plan.obj_user}"+
            "</if>"+
            "<if test='plan.plan_state!=null and plan.plan_state!=\"\"'>"+
                "and plan.plan_state=#{plan.plan_state}"+
            "</if>"+
            " order by create_time desc limit #{arg1},#{arg2}"+
            "</script>")
    public List<Plan> findPlan_CROTime(@Param("plan")Plan plan,int page,int total);//按创建时间逆序排序

    @Select("<script>" +
            "select * from plan where 1=1 and plan.pub_Id=#{plan.pub_Id}" +
            "<if test='plan.obj_user!=null and plan.obj_user!=\"\"'>"+
            "and plan.obj_user=#{plan.obj_user}"+
            "</if>"+
            "<if test='plan.plan_state!=null and plan.plan_state!=\"\"'>"+
            "and plan.plan_state=#{plan.plan_state}"+
            "</if>"+
            " order by over_time desc limit #{arg1},#{arg2}"+
            "</script>")
    public List<Plan> findPlan_OROTime(@Param("plan")Plan plan,int page,int total);//按结束时间逆序排序

    @Select("<script>" +
            "select * from plan where 1=1 and plan.pub_Id=#{plan.pub_Id}" +
            "<if test='plan.obj_user!=null and plan.obj_user!=\"\"'>"+
                "and plan.obj_user=#{plan.obj_user}"+
            "</if>"+
            "<if test='plan.plan_state!=null and plan.plan_state!=\"\"'>"+
                "and plan.plan_state=#{plan.plan_state}"+
            "</if>"+
            " order by over_time asc limit #{arg1},#{arg2}"+
            "</script>")
    public List<Plan> findPlan_COTime(@Param("plan") Plan plan,int page,int total);//按结束时间顺序排序

    @Select("<script>" +
            "select * from plan where 1=1 and plan.pub_Id=#{plan.pub_Id}" +
            "<if test='plan.obj_user!=null and plan.obj_user!=\"\"'>"+
            "and plan.obj_user=#{plan.obj_user}"+
            "</if>"+
            "<if test='plan.plan_state!=null and plan.plan_state!=\"\"'>"+
            "and plan.plan_state=#{plan.plan_state}"+
            "</if>"+
            " order by over_time asc limit #{arg1},#{arg2}"+
            "</script>")
    public List<Plan> findPlan_OOTime(@Param("plan")Plan plan,int page,int total);//按结束时间顺序排序
    //查询计划-通过名字模糊查询
    @Select("<script>" +
            "select * from plan where plan_name LIKE CONCAT('%',#{plan_name},'%') and pub_Id=#{pub_Id}" +
            "</script>")
    public List<Plan> findPlanByName(Plan plan);

    //查询计划-通过连接表名
    @Select("select * from plan where tab_Id=#{tab_Id}")
    public List<Plan> findPlanByTid(int tab_Id);

    @Select("select * from plan where plan_Id=#{plan_Id}")
    public Plan findPlanByPid(int plan_Id);

    //编辑计划
    @Update("update plan set plan_name=#{plan_name},create_time=#{create_time}," +
            "over_time=#{over_time},obj_user=#{obj_user},tab_Id=#{tab_Id}" +
            "plan_state=#{plan_state},t_text=#{t_text} where plan_Id=#{plan_Id}")
    public void updatePlan(Plan plan);

    //新增计划
    @Insert("insert into plan(pub_Id,plan_name,create_time,over_time,obj_user,tab_Id,plan_state,t_text) " +
            "values(#{pub_Id},#{plan_name},#{create_time},#{over_time},#{obj_user},#{tab_Id},#{plan_state},#{t_text})")
    public void addPlan(Plan plan);

    //删除计划
    @Delete("delete * from plan where plan_Id=#{plan_Id}")
    public void deletePlan(int plan_Id);


}
